package de.playmee;

import java.util.List;

import de.playmee.handler.MusicHandler;
import de.playmee.views.SimpleLibraryViewAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class LibraryActivity extends Activity {

	private final String TAG = "LibraryActivity";

	/**
	 * The music handler is stored globally in the application. It should be
	 * committed to the adapter, so that the adapter can get track data from it.
	 */
	private MusicHandler mHandler;

	/**
	 * This is the content view of this activity. It's a linear layout
	 * (vertical) with the header already included. So simply add the custom
	 * view and it will appear below the header.
	 */
	private LinearLayout layout;

	/**
	 * Definitions of the available custom views. Needed to simplify code if the
	 * OnItemClickListener should be set and if the user changes the layout. The
	 * default view is the SimpleListView.
	 */
	private final int SimpleListView = R.layout.library_simplelist;
	// TODO add more custom views here
	private final int defaultView = SimpleListView;

	/**
	 * This is the currently used customized view. By default it is the above
	 * defined defaultView.
	 */
	private int currentView = defaultView;

	/**
	 * called when the activity is first created
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set the content view
		setContentView(R.layout.library);
		layout = (LinearLayout) findViewById(R.id.library_layout);

		// load the music handler
		mHandler = ((Playmee) getApplication()).getMusicHandler();

		refreshView();
	}

	/**
	 * refreshes the View (needed if underlying data has been changed)
	 */
	private void refreshView() {
		if (!mHandler.hasMusic()) {
			// no music available
			createNothingToDisplayView();
		} else {
			// music tracks found
			AdapterView<?> view = null;
			
			// decide which custom view should be displayed
			switch (currentView) {
				case SimpleListView:
					view = createSimpleListView();
					break;
				// TODO add more custom views here
			}

			// set the OnItemClickListener to define what should be displayed if
			// the user clicks on an item in the view
			/*
			view.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					Track track = (Track) parent.getItemAtPosition(position);
					showActionsDialog(track);
				}
			});
			*/
		}
	}

	/**
	 * shows a simple text view to inform the user that no music is available
	 */
	private void createNothingToDisplayView() {
		Log.i(TAG, "Cannot create View. No music available.");

		// create text view and set its text
		TextView view = new TextView(this);
		view.setText("No music available.");

		// add view to the layout
		layout.addView(view);
	}

	/**
	 * shows a simple list view with image, artist, title and duration of the tracks
	 * 
	 * @return ListView
	 */
	private ListView createSimpleListView() {
		Log.i(TAG, "Creating view to display tracks...");

		// add the custom list view to the layout via layout inflater
		getLayoutInflater().inflate(R.layout.library_simplelist, layout);
		ListView view = (ListView) findViewById(R.id.library_simplelist);
		
		// create and set the adapter
		List<Track> tracks = mHandler.loadTracks(null, MusicHandler.ORDER_BY_ARTIST_TITLE_ABC, true);
		SimpleLibraryViewAdapter adapter = new SimpleLibraryViewAdapter(this, mHandler, tracks);
		view.setAdapter(adapter);

		Log.i(TAG, "Done creating view. Tracks are displayed.");
		return view;
	}
	
	/**
	 * shows a dialog with all actions for a track
	 * 
	 * @param position
	 */
	/*
	private void showActionsDialog(final Track track) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(android.R.drawable.ic_menu_manage);
		builder.setTitle(track.getTitle());
		
		final String DETAILS = "Show details";
		final CharSequence[] actions = { DETAILS };
		
		builder.setItems(actions, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
		    	CharSequence action = actions[item];
		    	
		    	// details
		    	if (action.equals(DETAILS)) {
		    		CustomDialog.showInfoDialog(LibraryActivity.this, "Details",
		    				track.getInfo());
		    	}
		    }
		});
		
		AlertDialog alert = builder.create();
		alert.show();
	}
	*/
}
